class catchUtil {
	//设置添加cookie
	setCookie:(name, value, {
		secure,
		domain,
		maxAge,
		path
	} = {}) => {
		let cookieText = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`
		if (typeof maxAge == 'number') {
			cookieText += `; maxAge=${maxAge}`
		}
		if (domain) {
			cookieText += `; domain=${domain}`
		}
		if (secure) {
			cookieText += `; secure=${secure}`
		}
		if (path) {
			cookieText += `; path=${path}`
		}
		document.cookie = cookieText;
		console.log(cookieText);
	},
	
	//获取cookie
	getCookie(name)=>{
		const decodedCookie = decodeURIComponent(document.cookie);
		const cookieList = decodedCookie.split("; ");
	
		for (let i = 0; i < cookieList.length; i++) {
			let cookie = cookieList[i];
	
			while (cookie.charAt(0) == " ") {
				cookie = cookie.substring(1);
			}
	
			if (cookie.indexOf(name + "=") == 0) {
				return cookie.substring(name.length + 1, cookie.length);
			}
		}
		return "";
	},
	
	
	//删除cookie
	deleteCookie(name)=>{
		  document.cookie = name + "=; expires=Thu, 01 Jan 2024 00:00:00 UTC; path=/;";
	},
	
	setLocalStorage:(key, val) => {
			if (key && val) {
				localStorage.setItem(key, JSON.stringify(val));
			}
		},

		getLocalStorage: (key) => {
			const val = localStorage.getItem(key)
			if (val) {
				return JSON.parse(val);
			}
		},

		setSessionStorage: (key, val) => {
			if (key && val) {
				SessionStorage.setItem(key, JSON.stringify(val));
			}
		},

		getSessionStorage: (key) => {
			const val = SessionStorage.getItem(key)
			if (val) {
				return JSON.parse(val);
			} else {
				return {}
			}
		}
}




export default catchUtil
